package com.alennus.blog.controller;

import com.alennus.blog.config.shiro.ShiroUtils;
import com.alennus.blog.domain.Account;
import com.alennus.blog.domain.Record;
import com.alennus.blog.response.WebApiResponse;
import com.alennus.blog.service.RecordService;
import com.alennus.blog.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by pactera on 2018/7/19.
 */
@RestController
@RequestMapping("record")
public class RecordController {



    @Autowired
    private RecordService recordService;


    @PostMapping("/saveOrUpdate")
    public WebApiResponse<Record> save(@Valid @RequestBody Record record){
        if(StringUtils.isEmpty(record.getId())){
            record.setCreateUserId(ShiroUtils.getUserId());
            record.setCreatedDate(DateUtils.getCurrentDatetime());
            record.setUpdateDate(DateUtils.getCurrentDatetime());
        }else{
            record.setUpdateUserId(ShiroUtils.getUserId());
            record.setUpdateDate(DateUtils.getCurrentDatetime());
        }

        return WebApiResponse.success(recordService.save(record));
    }

    @GetMapping
    public WebApiResponse<Page<Record>> getRecord(Record record, Pageable pageable){
        return WebApiResponse.success(recordService.findRecord(record,pageable));
    }

    @PostMapping("/delete")
    public WebApiResponse<Record> delete(@RequestBody List<Record> records){
        recordService.delete(records);
        return WebApiResponse.success();
    }

}
